package com.xjk.ggbp_back.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import com.xjk.ggbp_back.entity.CustomUser;
import com.xjk.ggbp_back.service.CustomUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;


@Slf4j
@RestController
@RequestMapping("/user")
public class CustomUserController {

    CustomUserServiceImpl customUserService;

    @Autowired
    public CustomUserController(CustomUserServiceImpl customUserService){
        this.customUserService = customUserService;
    }


//    模拟获取用户列表
    @GetMapping("")
    public ResponseEntity<?> getUsers() {
//        log.info("getUsers");

        CustomUser admin = new CustomUser();
        admin.setUsername("admin");
        admin.setPassword("admin");

        admin.setEmail("admin@qq.com");

        admin.setId(1L);


        CustomUser user = new CustomUser();
        user.setUsername("user");
        user.setPassword("user");

        user.setEmail("user@qq.com");

        user.setId(2L);

        JSONObject json = new JSONObject();
        json.put("code", 200);
        json.put("msg", "获取用户列表成功");
        json.put("data", Arrays.asList(admin, user));

        return ResponseEntity.ok(json);
    }

    /**
     * 根据用户ID获取用户角色及权限信息
     *
     * @param userId 用户唯一标识符（路径参数），不可为null
     * @return ResponseEntity<JSONObject> 包含操作状态码、消息和权限数据：
     *         - code: 200 表示成功
     *         - msg: 操作结果描述
     *         - data: 包含用户角色和权限的字符串列表
     *         当参数无效时返回400错误
     */
    @GetMapping("/permission/{userId}")
    public ResponseEntity<?> getUserRoleAndPermission(@PathVariable Integer userId) {
        // 参数有效性检查
        if (userId == null){
            return ResponseEntity.badRequest().build();
        }

        // 根据用户ID分配权限模板
        if(userId==1){
            JSONObject json = new JSONObject();
            json.put("code", 200);
            json.put("msg", "获取用户角色和权限成功");
            // 管理员权限模板
            json.put("data", Arrays.asList("admin", "user", "admin:add", "admin:delete", "admin:update", "admin:query"));
            return ResponseEntity.ok(json);
        }else {
            JSONObject json = new JSONObject();
            json.put("code", 200);
            json.put("msg", "获取用户角色和权限成功");
            // 普通用户权限模板
            json.put("data", Arrays.asList("user", "user:add", "user:delete", "user:update", "user:query"));
            return ResponseEntity.ok(json);
        }
    }


}
